﻿namespace GFCTP
{
	internal class KDJ : Indicator
	{
		private readonly BarSeries C;
		private readonly int Length; //, SlowLength, SmoothLength;

		public BarSeries DValue;

		private BarSeries HH;

		public BarSeries JValue;

		public BarSeries KValue;
		private BarSeries LL;
		private BarSeries RSV;

		public KDJ(int length, BarSeries h, BarSeries l, BarSeries c) : base(null, length)
		{
			this.C = c;
			this.Length = length;
		}

		public override void OnBarUpdate()
		{
			this.RSV[0] = (this.C[0] - this.LL[0])/(this.HH[0] - this.LL[0])*100;
			if (CurrentBar < this.Length)
			{
				this.KValue[0] = 50;
				this.DValue[0] = 50;
			}
			else
			{
				this.KValue[0] = this.KValue[1]*2/3 + this.RSV[0]/3;
				this.DValue[0] = this.DValue[1]*2/3 + this.KValue[0]/3;
			}
			this.JValue[0] = this.KValue[0]*3 - this.DValue[0]*2;
		}
	}
}
